package com.dennytech.tac.utils.db;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dennytech.tac.model.Togo;

public class TogoDBHelper extends DBHelper<Togo> {
	private static TogoDBHelper instance;

	public static TogoDBHelper instance() {
		return instance;
	}

	public static void init(Context c) {
		if (instance == null) {
			instance = new TogoDBHelper(c);
		}
	}

	public TogoDBHelper(Context c) {
		super(c, "togo");
	}

	@Override
	protected SQLiteDatabase createDataBase(Context c) throws IOException {
		File path = new File(dbDir(c), dbName());
		SQLiteDatabase db = null;
		try {
			db = SQLiteDatabase.openOrCreateDatabase(path, null);
		} catch (Exception e) {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" (");
		sb.append("id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		sb.append("attid INTEGER, ");
		sb.append("attdb TEXT, ");
		sb.append("status INTEGER);");

		db.execSQL(sb.toString());
		return db;
	}

	@Override
	protected String dbName() {
		return "togo.db";
	}

	@Override
	protected File dbDir(Context c) {
		return c.getDir("database", Context.MODE_PRIVATE);
	}

	@Override
	public List<Togo> query() {
		List<Togo> list = new ArrayList<Togo>();
		try {
			String query = "SELECT * FROM " + tableName + " ORDER BY id ;";
			Cursor c = db.rawQuery(query, null);
			if (c.moveToFirst()) {
				do {
					Togo togo = new Togo();
					togo.setId(c.getInt(0));
					togo.setAttId(c.getInt(1));
					togo.setAttDb(c.getString(2));
					togo.setStatus(c.getInt(3));
					list.add(togo);

				} while (c.moveToNext());
				c.close();
			} else {
				c.close();
				return null;
			}
		} catch (Exception e) {
			return null;
		}
		return list;
	}

	public Togo query(String aid) {
		Togo togo = null;
		try {
			String query = "SELECT * FROM " + tableName + " WHERE attid = "
					+ aid + " ;";
			Cursor c = db.rawQuery(query, null);
			if (c.moveToFirst()) {
				togo = new Togo();
				togo.setId(c.getInt(0));
				togo.setAttId(c.getInt(1));
				togo.setAttDb(c.getString(2));
				togo.setStatus(c.getInt(3));

				c.close();
			} else {
				c.close();
				return null;
			}
		} catch (Exception e) {
			return null;
		}
		return togo;
	}

	public List<Togo> query(int status) {
		List<Togo> togoList = new ArrayList<Togo>();
		try {
			String query = "SELECT * FROM " + tableName + " WHERE status = "
					+ status + " ;";
			Cursor c = db.rawQuery(query, null);
			if (c.moveToFirst()) {
				do {
					Togo togo = new Togo();
					togo.setId(c.getInt(0));
					togo.setAttId(c.getInt(1));
					togo.setAttDb(c.getString(2));
					togo.setStatus(c.getInt(3));
					togoList.add(togo);

				} while (c.moveToNext());
				c.close();
			} else {
				c.close();
			}
		} catch (Exception e) {
			return null;
		}
		return togoList;
	}

	@Override
	public void insert(Togo t) {
		ContentValues cv = new ContentValues();
		cv.put("attid", t.getAttId());
		cv.put("attdb", t.getAttDb());
		cv.put("status", t.getStatus());
		db.insert(tableName, null, cv);
	}

	@Override
	public int delete(Togo t) {
		return db.delete(tableName, "id = " + t.getId(), null);
	}
	
	public int delete(int attId) {
		return db.delete(tableName, "attId = " + attId, null);
	}

	@Override
	public int update(Togo t) {
		ContentValues cv = new ContentValues();
		cv.put("status", t.getStatus());
		return db.update(tableName, cv, "id = " + t.getId(), null);
	}
}
